
import tkinter as tk
from user_info import UserInfo
from main_window import MainWindow
from my_menu import MyMenu
from tkinter import messagebox
from menu_type import MenuType
class LoginWindow:
    def __init__(self, root):
        # 创建登录窗口界面
        self.root = root
        root.title("用户登录")
        root.geometry("300x200")
        # 使用Frame容器化管理组件
        iframe = tk.Frame(root, width=300, height=200, relief='ridge')
        iframe.grid(padx=20, pady=30)
        # 系统标题
        sys_label = tk.Label(iframe, text="学生成绩管理系统",fg="green",font=("华文行楷",20))
        sys_label.grid(row=0, column=0, columnspan=2,pady=10)
        # 用户名输入
        name_label = tk.Label(iframe, text="用户名：")
        self.name_entry = tk.Entry(iframe)
        name_label.grid(row=1, column=0, sticky="e")
        self.name_entry.grid(row=1, column=1, sticky="w")
        # 密码的输入
        pwd_label = tk.Label(iframe, text="密码：", padx=10)
        self.pwd_entry = tk.Entry(iframe, show="*")
        pwd_label.grid(row=2, column=0, sticky="e")
        self.pwd_entry.grid(row=2, column=1, sticky="w")
        self.pwd_entry.bind("<Return>", lambda event: self.login())
        # 登录按钮
        login_button = tk.Button(iframe, text="登录", width=10, command=self.login)
        login_button.grid(row=3, column=0, columnspan=2)
        root.mainloop()

    def login(self):
        print("--------------")
        # 获取用户名和密码
        username = self.name_entry.get()
        password = self.pwd_entry.get()
        # 验证用户名和密码
        if self.validate_login(username,password):
            # 登录成功，弹出提示框
            tk.messagebox.showinfo("登录成功", "欢迎登录")
            # 销毁当前窗口，打开主界面
            self.root.destroy()
            # 创建主界面窗口
            main_window = tk.Tk()
            #MainWindow(main_window,username)
            # 创建菜单
            MyMenu(main_window,sub_type=MenuType.MENU_HOME,login_name=username)

            # main_window.mainloop()

        else:
            # 登录失败，弹出提示框
            tk.messagebox.showerror("登录失败", "用户名或密码错误")
    def validate_login(self,username,pwd):
        #登录操作
        return UserInfo().login(username,pwd)
if __name__ == "__main__":
    # 创建主窗口
    root = tk.Tk()
    # 创建登录窗口
    login_window = LoginWindow(root)
    # 运行主循环
    root.mainloop()